package cn.xiaopengstack.infrastructure.persistent.strategy.notify;

import cn.xiaopengstack.domain.trade.model.entity.NotifyTaskEntity;
import cn.xiaopengstack.domain.trade.model.valobj.NotifyTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.ConcurrentMap;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author jiangyangang
 */
@Component
@Slf4j
public class NotifyStrategyFactory {

    private final ConcurrentMap<NotifyTypeEnum, INoticeStrategyService> noticeStrategyServicesMap;

    public NotifyStrategyFactory(List<INoticeStrategyService> noticeStrategyServices) {
        noticeStrategyServicesMap = noticeStrategyServices.stream().collect(Collectors.toConcurrentMap(INoticeStrategyService::type, Function.identity(), (s1, s2) -> s1));
    }


    public AbstractNotifyStrategy<?> getNoticeService(NotifyTypeEnum typeEnum, String scene){
        switch (typeEnum) {
            case MQ -> {
                return new MQNotifyStrategy(scene);
            }
            case RPC -> {
                return new RPCNotifyStrategy(scene);
            }
            case HTTP -> {
                return new HttpNotifyStrategy(scene);
            }
            default -> {
                return null;
            }
        }
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    public static class DynamicContext{

        /**
         * 通知实体
         */
        private NotifyTaskEntity notifyTaskEntity;

    }


}
